<template>
	<view class="address">
		<view class="uni-list">
			<view class="uni-list-cell ListBox" v-for="(item,index) in addressList" :key="index" @tap="goBack(item)">
				<view class="title">
					<text class="name">{{item.userName}}</text> <text class="phone">{{item.mobile}}</text>
				</view>
				<view class="addressInfo">
					{{item.address + item.detailAddress}}
				</view>
				<view class="btns">
					<view>
						<view class="isDefault" v-if="item.defaultAddress == 1">
							<image src="../../static/images/two_circle.png" mode="widthFix"></image>默认地址
						</view>
					</view>
					
					<view class="btnbox">
						<view class="edit" @tap.stop="goEdit(item)">
							编辑
						</view>
						<view class="del" @tap.stop="del(item)">
							删除
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="addAddress" @tap="addAddress">
			<image src="../../static/images/add.png" mode="widthFix"></image>
			添加收货地址
		</view>
	</view>
</template>

<script>
	export default{
		data() {
			return {
				addressList:[],
				isBack:false
			}
		},
		onLoad(e) {
			if(e.type == 1){
				this.isBack = true
			}
			this.getData()
		},
		onShow(){
			this.getData()
		},
		methods:{
			getData(){
				let data = {}
				this.$http.post('/api/v1.0/user/address/all/address',data,res=>{
					this.addressList = res.data.data;
				})
			},
			goEdit(item){
				uni.navigateTo({
					url:'editAddress?data='+ encodeURIComponent(JSON.stringify(item))
				})
			},
			del(item){
				var _this = this;
				uni.showModal({
					content: '确定删除此地址?',
					confirmColor: "#F63810",
				    success: function (res) {
				        if (res.confirm) {
							let data = {
								  "addressId": item.addressId,
								  "defaultAddress": item.defaultAddress,
								  "userName": item.userName,
							}
							_this.$http.post('/api/v1.0/user/address/delete/address',data,res=>{
								console.log(res)
								if(res.data.data.msg == "000000"){
									uni.showToast({
										title:"删除成功！",
										icon:"none"
									})
								}else{
									uni.showToast({
										title:"删除失败，请稍后再试！",
										icon:"none"
									})
								}
								_this.getData()
								
							})
							
				        } else if (res.cancel) {
				            uni.showToast({
				            	title:"已取消删除",
				            	icon:"none"
				            })
				        }
				    }
				});
			},
			addAddress(){
				uni.navigateTo({
					url:"editAddress"
				})
			},
			goBack(item){
				if(this.isBack == false){
					return
				}
				uni.$emit("address",item);
				uni.navigateBack()
			}
		} 
	}
</script>

<style lang="scss">
	.address{
		background-color: #f2f2f2;
		min-height: 100vh;
		padding-top: 40upx;
		color: #333;
		box-sizing: border-box;
		.uni-list{
			width: 694upx;
			margin: 0 auto;
			.ListBox{
				background-color: #fff;
				margin-bottom: 20upx;
				border-radius: 30upx;
				padding: 30upx;
				box-sizing: border-box;
				.title{
					.name{
						font-size: 28upx;
						margin-right: 10upx;
					}
					.phone{
						font-size: 24upx;
					}
				}
				.addressInfo{
					margin-top: 20upx;
					font-size: 24upx;
					padding-bottom: 20upx;
					border-bottom: 1upx solid #f2f2f2;
				}
				.btns{
					margin-top: 20upx;
					display: flex;
					justify-content: space-between;
					font-size: 24upx;
					.isDefault{
						display: flex;
						align-items: center;
						image{
							width: 22upx;
							height: 22upx;
							margin-right: 10upx;
						}
					}
					.btnbox{
						display: flex;
						justify-content: flex-end;
						view{
							border: 1upx solid #333;
							border-radius: 10upx;
							padding: 10upx 30upx;
							margin-left: 20upx;
						}
					}
				}
			}
		}
		.addAddress{
			display: flex;
			align-items: center;
			width: 694upx;
			margin: 40upx auto;
			line-height: 80upx;
			border-bottom: 1upx solid #d9d9d9;
			image{
				width: 37upx;
				height: 37upx;
				margin-right: 15upx;
			}
			font-size: 28upx;
			color: #bfbfbf;
		}
	}
</style>
